<?php

/**
 * @author игорь
 * @copyright 2009
 */

include "root.php";
include ROOT_DIR."/conf/conf.php";
$user = new User;
$com = new CommentWizard;
if(isset($_POST['comment']))
{
	$comment = $_POST['comment'];
	unset($_POST['comment']);
	if(!$user->user_id)
	{
		if(isset($comment['control_result']) and isset($_SESSION['control_summ']) and intval($comment['control_result']) != 0 and intval($_SESSION['control_summ']) != 0 and intval($_SESSION['control_summ']) == intval($comment['control_result']))
		{
			$insert_comment = array(
				'pid' => (intval($comment['pid']) != 0?intval($comment['pid']):0),
				'user_id' => 0,
				'module' => $comment['module'],
				'obj_id' => $comment['obj_id'],
				'ip' => $user->ip,
				'comment' => strval($comment['comment']),
				'nickname' => (strval($comment['nickname']) != ''?strval($comment['nickname']):'Аноним'),
				'theme' => strval($comment['theme']),
				'time' => time(),
				'status' => 2
			);
		}
		else
		{
			$_SESSION['message'] = "Не правильно указан результат для контрольных цифр";
		}
	}
	else
	{
		$insert_comment = array(
			'pid' => (intval($comment['pid']) != 0?intval($comment['pid']):0),
			'user_id' => $user->user_id ,
			'module' => $comment['module'],
			'obj_id' => $comment['obj_id'],
			'ip' => $user->ip,
			'comment' => strval($comment['comment']),
			'nickname' => $user->login,
			'theme' => strval($comment['theme']),
			'time' => time(),
			'status' => 2
		);
	}
	if(isset($insert_comment))
	{
		$res = $db->insert($com->table,$insert_comment);
		if($res)
		{
			$_SESSION['message'] = "Спасибо за комментарий";
		}
		else
		{
			$_SESSION['message'] = "Извините, ошибка при добавлении комментария. Администрация сайта уже уведомлена о случившемся.";
		}
	}
	if(strval($comment['source']) != '')
	{
		header('location: '.$comment['source']);die();
	}
	else
	{
		header('location: '.SITE_URL);die();
	}
}

?>